#include<vector>
using namespace std;
class Solution {
public:
    vector<int> prevPermOpt1(vector<int>& A) {
        if (A.size() < 2) return A;
        bool flag = true;
        int index = 0;
        for (index = A.size() - 2; index >= 0; --index) {
            if (A[index] > A[index + 1])
                break;
        }
        if (index < 0) return A;
        int last = 0;
        for (last = index + 1; last < A.size(); ++last) {
            if (A[last] >= A[index]) {
                break;
            }
        }
        --last;
        if (last == index) return A;
        while (A[last - 1] == A[last])--last;
        swap(A[index], A[last]);
        return A;
    }
};